package module::gucci_uk::default;
use strict;
use warnings;
use Encode;

use vars qw($VERSION @ISA @EXPORT);
$VERSION = 1.00;
@ISA     = qw(Module::Module);
@EXPORT  = ();
my $data = $main::filedata;

# ----------------------------------------------------
# constructor
# Modify Log
# 12/04/2012 Alex create
# ----------------------------------------------------
sub new {
	my $class = shift;
	my %parm  = @_;
	my $this  = {};
	bless $this, $class;
	return $this;
}

# ----------------------------------------------------
# get all product's url and categories then write them
# into temp xml
# Modify Log
# 12/04/2012 Alex create
# ----------------------------------------------------

sub filter {

	#A200000 Filter retailer website starts
	Log::Write::setLog('A200000');
	my $this     = shift;
	my $writexml = new Feed::XML();
	my $data    = $main::filedata;
	my $hostUrl = $this->{'config'}->{'host_url'};
	my $catalog = { 'Product' => [] };
	my $charset  = 'utf-8';
	
	# get entryUrls
	my %entryUrlList;
	%entryUrlList = &getCategoryUrls( $hostUrl, $charset);
	
	if ( !%entryUrlList ) {
		# E200000: filter failed
		Log::Write::setLog('E200000');
		return;
	} else {
		# A200003: The total count of the entryUrl obtained
		Log::Write::setLog('A200003', { 'COUNT' => scalar(keys(%entryUrlList))});
	}
	
	# get all product links
	my @allProducts = &getFilterProduct( $charset, $hostUrl, \%entryUrlList);	
	
	# write tree date into XML file
	if (@allProducts) {
		$catalog->{'Product'} = \@allProducts;
		$writexml->addXML( $catalog, $this->{'config'}->{'data_filter_file'} );

		# A200002the total count of detail page urls
		Log::Write::setLog('A200002', { 'COUNT' => scalar(@allProducts) } );
	} else {
		# E200000filter failed
		Log::Write::setLog('E200000');
		return;
	}

	# A299999Filter retailer website end.
	Log::Write::setLog('A299999');
	
}

# ----------------------------------------------------
# parse all products detail page and write the infos
# into final XML feed
# Modify Log
# 12/04/2012 Alex create
# ----------------------------------------------------
sub parser {

	# A300000Parser detail page starts.
	Log::Write::setLog('A300000');
	my $this         = shift;
	my $hostUrl = $this->{'config'}->{'host_url'};
	
	# new parser object
	my $parserObj = module::gucci_uk::parser->new;
	
	# new $writexml object, used for read and write XML info
	my $writeXml = new Feed::XML();

	#read xml create by filter to get all products infos(url and category)
	my @xmlInfo = $writeXml->readXML( $this->{'config'}->{'data_filter_file'} );
	
	my $count = 0;
	my $total = 0;

	# $catalog is used for recording a set of products
	my $catalog = { 'Product' => [] };

	my $affiliate_feed_gz_path = $this->{'config'}->{'affiliate_feed_gz_path'};
	my $affiliate_feed_file = $this->{'config'}->{'affiliate_feed_file'};

	my $line;
	my @allContent;
	if ( -e $affiliate_feed_gz_path ) {
		system("gunzip -c $affiliate_feed_gz_path > $affiliate_feed_file");
		open( RD, $affiliate_feed_file );
		while ( $line = <RD> ) {
			push @allContent, decode( 'utf8', $line );
		}
		close(RD);		
	 }else{
		# E000003 : Can't find the affiliate feed of unzip.
		Log::Write::setLog( 'E000003');
		return; 		 		
	 }
	shift @allContent;
	Log::Write::setLog( "A300001", { 'allCOUNT' => scalar @allContent } );		
	
	my %affproductsHash;
	foreach my $oneProductInfo (@allContent) {
		if( $oneProductInfo =~ /sku_number="([^"]*?)".*?<product>([^<]*?)<\/product>/is ){
			my $skuNum = $1;
		    my $productAffUrl = $2;
			$skuNum =~ s/\s+//g;	
			$affproductsHash{$skuNum} = $productAffUrl;			
		}
	}

	my $productCheckString;
	for my $oneTemp (@xmlInfo) {

		# %ProductInfo : one product info from *_filter.xml
		my %oneTempInfo = %{$oneTemp};
		my $url            = ( @{ $oneTempInfo{"ProductURL"} } )[0];
		my $productId         = ( @{ $oneTempInfo{"ProductId"} } )[0];
		my %product;
        my %productInfo;
		if( !$affproductsHash{$productId} ){
			next;
		}

		if( $productCheckString =~ /\Q$productId\E<split>/is ){
			next;
		}else{
			$productCheckString = $productCheckString.$productId."<split>";
		}	
			
		eval {
			# get infos from product detail page and save it in hash variable
			%productInfo = $parserObj->parseAndgetInfo( $url, $hostUrl );
		};
		if ($@) {
			# E300003 : Failed to extract information from XXX page.
			Log::Write::setLog( "E300003", { 'URL' => $url } );
			next;
		}
		
		if($productInfo{"productCheckString"}){
			$productCheckString = $productCheckString.$productInfo{"productCheckString"};
		}

		my $productUrlFeed = $affproductsHash{$productId};
		my $brand          = 'Gucci';
		if($productInfo{"Name"}){
			my $product = {
				'Retailer'    => $this->{'config'}->{'retailer_name'},
				'ProductId'   => $productInfo{"ProductId"},
				'Name'        => $productInfo{"Name"},
				'Brand'       => $brand,
				'Price'       => $productInfo{"Price"},
#				'SalePrice'   => $productInfo{"SalePrice"},
				'Description' => $productInfo{"Description"},
				'ProductURL type="direct"'  => $productInfo{"ProductURL"},
				'ProductURL type="feed"' => $productUrlFeed,
				'ImageURL'    => $productInfo{"ImageURL"},
				'Category'    => $oneTempInfo{"Category"},
				'Size'        => $productInfo{"Size"},
				'Color'       => $productInfo{"Color"},
				'Time'        => $productInfo{"Time"}
			};
			
			$catalog->{'Product'} = [ @{ $catalog->{'Product'} }, $product ];
			
			$count++;
			$total++;
			# write data into XML per 100(configurable) records 
			if ($count == $this->{'config'}->{'write_frequency'}) {
				$writeXml->addXML( $catalog, $this->{'config'}->{'data_feed_file'} );
				$catalog     = { 'Product' => [] };
				# A300001 : Record XXX prodcuts have been appended to feed file.
				Log::Write::setLog( "A300001", {'COUNT'=>$count});
				$count = 0;
			}			
		}
	}
	$writeXml->addXML( $catalog, $this->{'config'}->{'data_feed_file'} );
	# A300001 : Record XXX prodcuts have been appended to feed file.
	Log::Write::setLog( "A300001", {'COUNT'=>$count});
	
	if ($total) {
		# A300003 : Record XXX prodcuts have been written into feed file totally.
		Log::Write::setLog('A300003', {'COUNT'=>$total});
		# A399999 : Parser detail page end.
		Log::Write::setLog('A399999');
	} else {
		# E300000 : Parser failed.
		Log::Write::setLog('E300000');
	}
}

# ----------------------------------------------------
# get all category urls
# Modify Log
# 12/04/2012 Alex create
# ----------------------------------------------------

sub getCategoryUrls {

	my $url     = shift;
	my $charset = shift;
	my $data    = $main::filedata;
	my %entryUrlList;
	
	my $content = $data->getPage( $url, $charset );

	# get page failed
	if ( !$content ) {
		# E200001 Failed to get XX entrance information.
		Log::Write::setLog( 'E200001', { 'URL' => $url } );
		return;
	}
	
	my $category_content;

	if ($content =~ /<nav id="header_megas">(.*?)<\/nav>/is) {
		$category_content = $1;
	}else{
		# E200005 : Failed to get the main url of the website .
		Log::Write::setLog('E200005',{'URL'=>$url});
		return;
	}

	while( $category_content =~ /<li class="mega_menu">\s*<span class="mega_link">([^<]*?)<\/span>(.*?<\/div>)\s*<\/li>/isg ){
		my $firstcategory = $1;
		my $seccat_content = $2;
		$firstcategory =~ s/^\s*|\s*$//isg;
		
		while ( $seccat_content =~ /<a href="([^"]*?)"[^>]*?>([^<]*?)<\/a>/isg ){
			my $secondcat_url = $1;		
			my $secondcategory = $2;
			$secondcategory =~ s/^\s*|\s*$//isg;
			$entryUrlList{$secondcat_url}	= $firstcategory."<split>".$secondcategory;
			Log::Write::setLog( 'A200001', { 'category_url' => $secondcat_url, "category"=>$firstcategory."<split>".$secondcategory } );	
			
			my $thirdcontent = $data->getPage($secondcat_url,$charset);
			
			if ( $thirdcontent =~ /<section id="sub_nav">(.*?)<\/section>/is ){
				my $thicat_content = $1;
				
				while ( $thicat_content =~ /<a href="([^"]*?)" class="category[^"]*?"[^>]*?>([^<]*?)<\/a>/isg ){
					my $thirdcat_url = $1;		
					my $thirdcategory = $2;
					if ( $thirdcategory =~ /View all/is ){
						next;
					}					
					$thirdcategory =~ s/^\s*|\s*$//isg;
					$entryUrlList{$thirdcat_url}	= $firstcategory."<split>".$secondcategory."<split>".$thirdcategory;
					Log::Write::setLog( 'A200001', { 'category_url' => $thirdcat_url, "category"=>$firstcategory."<split>".$secondcategory."<split>".$thirdcategory } );						
				}
			}else{
				# E200005 : Failed to get the main url of the website .
				Log::Write::setLog('E200005',{'URL'=>$secondcat_url});
				next;		
			}		
		}
		
		while ( $seccat_content =~ /<a href="([^"]*?)"><img alt="([^"]*?)"/isg ){
			my $secondcat_url = $1;
			my $secondcategory = $2;
			$secondcategory =~ s/^\s*|\s*$//isg;
			if ( !$entryUrlList{$secondcat_url} ){
				$entryUrlList{$secondcat_url}	= $firstcategory."<split>".$secondcategory;
				Log::Write::setLog( 'A200001', { 'category_url' => $secondcat_url, "category"=>$firstcategory."<split>".$secondcategory } );									
			}else{
				next;
			}		
			my $thirdcontent = $data->getPage($secondcat_url,$charset);
			
			if ( $thirdcontent =~ /<section id="sub_nav">(.*?)<\/section>/is ){
				my $thicat_content = $1;
				
				while ( $thicat_content =~ /<a href="([^"]*?)" class="category[^"]*?"[^>]*?>([^<]*?)<\/a>/isg ){
					my $thirdcat_url = $1;		
					my $thirdcategory = $2;
					if ( $thirdcategory =~ /View all/is ){
						next;
					}
					$thirdcategory =~ s/^\s*|\s*$//isg;
					$entryUrlList{$thirdcat_url}	= $firstcategory."<split>".$secondcategory."<split>".$thirdcategory;
					Log::Write::setLog( 'A200001', { 'category_url' => $thirdcat_url, "category"=>$firstcategory."<split>".$secondcategory."<split>".$thirdcategory } );						
				}
			}else{
				# E200005 : Failed to get the main url of the website .
				Log::Write::setLog('E200005',{'URL'=>$secondcat_url});
				next;		
			}		
		}		
		
    }	
	return %entryUrlList;

}

# ----------------------------------------------------
# return Array reference
# Modify Log
# 12/04/2012 Alex create
# ----------------------------------------------------
sub getFilterProduct {

	my $charset     = shift;
	my $hostUrl		= shift;
	my $pageUrls    = shift;
	my $data    = $main::filedata;
	my %pageUrlList = %{$pageUrls};
	my @allProducts;

	for my $pageUrl (keys(%pageUrlList)) {
		# paginal instock product count
		my $productCount = 0;

		my %product;
		my $category;
		my $tempPageUrl = $pageUrl;
		$tempPageUrl =~ s/&amp;/&/isg;
		my $content = $data->getPage($tempPageUrl, $charset);
		if ( !$content) {
			# E200005 Failed to get the main url of the website .
			Log::Write::setLog( 'E200005', { 'URL' => $pageUrl } );
			next;
		}
		
		my @categorys = split('<split>', $pageUrlList{$pageUrl});
		
		my $products_content;
		if ($content =~ /<div id="panel_wrapper">(.*?<\/div>\s*<\/div>)\s*<\/div>/is) {
			$products_content = $1;
			$products_content =~ s/&quot;/"/isg;
			$products_content =~ s/&lt;/</isg;
			$products_content =~ s/&gt;/>/isg;
		}
		while ($products_content =~ /<div class="product_info"[^>]*?>\s*<h3>\s*<a href="([^"]*?)">([^<]*?)<\/a>/isg) {
			my $productUrl = $1;
			#get product id
			my $productId;
			if ($productUrl =~ /\/([^\/]*?)$/is) {
				$productId = $1;
			}
			if ( $productId ){				
				my $product = {'ProductId' => $productId,
								'ProductURL'=> $productUrl,
								'Category'=>[{'Part' => \@categorys}]};
				push( @allProducts, $product );
				$productCount++;
			}
		}
		# A200001: Found XXX piece URLs of detail page through XXX entrance
		Log::Write::setLog( 'A200001', {'COUNT' => $productCount, 'URL' => $pageUrl});
	}
	
	return @allProducts;	
}

1;